package net.arvin.mobilebanksystem.fragment;

import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.activity.UserInfoActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RetrievePSWFragment extends Fragment implements OnClickListener {
	private EditText phonenumber_et;
	private EditText checkcode_et;
	private Button getcode_btn;
	private Button next_btn;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_retrievepsw, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initView(view);
		initListener();
	}

	private void initListener() {
		getcode_btn.setOnClickListener(this);
		next_btn.setOnClickListener(this);
	}

	private void initView(View view) {
		phonenumber_et = (EditText) view.findViewById(R.id.phonenumber);
		checkcode_et = (EditText) view.findViewById(R.id.checkcode);
		getcode_btn = (Button) view.findViewById(R.id.getcode);
		next_btn = (Button) view.findViewById(R.id.next);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.getcode:
			if (phonenuberValidate()) {
				Toast.makeText(getActivity(), "getcode clicked", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		case R.id.next:
			checkcodeValidate();
			break;
		}
	}

	private void checkcodeValidate() {
		String checkcode = checkcode_et.getText().toString().trim();
		if (checkcode == null || checkcode.equals("")) {
			Toast.makeText(getActivity(), "验证码不能为空~", Toast.LENGTH_SHORT)
					.show();
		}
		startResetPSWFragment();
	}

	private void startResetPSWFragment() {
		UserInfoActivity.userinfoActivity.setTitleTv("修改密码");
		ResetPSWFragment resetPSW = new ResetPSWFragment();
		resetPSW.setPhonenumber(phonenumber_et.getText().toString().trim());
		UserInfoActivity.userinfoActivity.switchContent(resetPSW);
	}

	private boolean phonenuberValidate() {
		String input_tel = phonenumber_et.getText().toString().trim();
		if (input_tel == null || input_tel.equals("")) {
			Toast.makeText(getActivity(), "请输入手机号~", Toast.LENGTH_SHORT).show();
			return false;
		} else {
			if (input_tel.length() != 11) {
				Toast.makeText(getActivity(), "手机号长度不对~", Toast.LENGTH_SHORT)
						.show();
				return false;
			}
		}
		return true;
	}
}
